﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Threading;
using System.Xml.Linq;

namespace Chat_WebServer.Controllers
{
    public class MessageController : ApiController
    {
        // Lay tin nhan theo ten
        public List<string> Get(string user)
        {
            int count = 0;
            List<string> messages = new List<string>();
            while (count < 30)
            {
                messages = GetMessagesByUsername(user);
                if (messages.Count > 0)
                    return messages;
                else
                    Thread.Sleep(1000);
                count++;
            }
            return null;
        }

        // Lay tin nhan theo ten
        private List<string> GetMessagesByUsername(string username)
        {
            using (var db = new AccountEntities())
            {
                List<string> messages = new List<string>();
                var user = db.Messages.ToList().FirstOrDefault(a => a.UserName == username);
                XElement xMessages = XElement.Parse(user.Messages);
                foreach (XElement fromUser in xMessages.Elements())
                {
                    foreach (XElement message in fromUser.Elements())
                    {
                        if (message.Attribute("new").Value == "1")
                        {
                            message.Attribute("new").Value = "0";
                            messages.Add(fromUser.Attribute("name").Value + "@" + message.Attribute("mess").Value);
                        }
                    }
                }
                if (messages.Count > 0)
                {
                    user.Messages = xMessages.ToString();
                    db.SaveChanges();
                }
                return messages;
            }
        }

        // Gui tin nhan
        public string POST(string fromUser, string toUser, string mess)
        {
            using (var db = new AccountEntities())
            {
                //Tao element cho message
                XElement message = new XElement("message");
                message.SetAttributeValue("new", "1");
                message.SetAttributeValue("mess", mess);

                // Tim trong database
                var user = db.Messages.ToList().FirstOrDefault(a => a.UserName == toUser);
                XElement xMessages = XElement.Parse(user.Messages);

                // Them message vao database neu co san nguoi gui
                foreach (XElement fromuser in xMessages.Elements())
                {
                    if (fromuser.Attribute("name").Value == fromUser)
                    {
                        fromuser.Add(message);
                        user.Messages = xMessages.ToString();
                        db.SaveChanges();
                        return mess;
                    }
                }

                // Tao element cho nguoi gui
                XElement friend = new XElement("friend");
                friend.SetAttributeValue("name", fromUser);

                //Them message
                friend.Add(message);

                //Them nguoi gui
                xMessages.Add(friend);

                //Luu vao database
                user.Messages = xMessages.ToString();
                db.SaveChanges();
                return mess;
            }
        }
    }
}
